package main

import "fmt"

func main() {
	var l1 = ListNode{
		Val:2,
		Next:&ListNode{
			Val:4,
			Next:&ListNode{
				Val:5,
				Next:nil,
			},
		},
	}

	var l2 = ListNode{
		Val:5,
		Next:&ListNode{
			Val:6,
			Next:&ListNode{
				Val:4,
				Next:nil,
			},
		},
	}


	fmt.Printf("%v\n", addTwoNumbers(&l1, &l2))
}

type ListNode struct {
	Val int
	Next *ListNode
 }

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func addTwoNumbers(l1 *ListNode, l2 *ListNode) (*ListNode) {
	var ll1 = l1
	var ll2 = l2
	var t *ListNode
	var rtl = new(ListNode)

	l1Sum := 0
	l2Sum := 0
	for i := l1; i !=nil ;i = i.Next  {
		l1Sum++
	}

	for i := l2; i !=nil ;i = i.Next  {
		l2Sum++
	}

	if l1Sum < l2Sum {
		ll1 = ll2
		ll2 = l1
	}

	var carry, curNum int
	if ll1 != nil {
		curNum = 0

		if ll2 != nil {
			curNum =ll1.Val + ll2.Val

			if curNum >= 10 {
				curNum = curNum % 10 + carry

				carry = curNum / 10
			}
			ll2 = ll2.Next
		} else {
			curNum = ll1.Val
		}

		t = new(ListNode)
		t.Val = curNum
		t.Next = nil

		if rtl.Val != 0 {
			rtl.Next = t
		} else {
			rtl = t
		}

		ll1 = ll1.Next
	}


	return rtl
}